package com.yunkaweilai.android.view.dialog.shop;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.flyco.dialog.utils.CornerUtils;
import com.flyco.dialog.widget.base.BaseDialog;
import com.github.lazylibrary.util.ToastUtils;
import com.yunkaweilai.android.R;
import com.yunkaweilai.android.activity.operation.integral.IntegralTypeActivity;
import com.yunkaweilai.android.activity.shop.ShopTypeActivity;
import com.yunkaweilai.android.base.BaseActivity;
import com.yunkaweilai.android.constants.Api;
import com.yunkaweilai.android.constants.CommonConstants;
import com.yunkaweilai.android.model.Event;
import com.yunkaweilai.android.net.HttpRequest;
import com.yunkaweilai.android.net.PostProcess;
import com.yunkaweilai.android.utils.InputMethodUtils;
import com.yunkaweilai.android.utils.Utils;

import org.greenrobot.eventbus.EventBus;

/**
 * Created by Administrator on 2017/6/27.
 */

public class ShopTypeDelConfirmDialog1 extends BaseDialog<ShopTypeDelConfirmDialog1> {
    TextView idTvRight;
    private Context context;
    private String id;
    private String type;
    private ShopTypeUpdateDialog1 dialogs;
    private ImageView idImgClose;
    private TextView idTvClose;
    private TextView idTvDelInfo;

    public ShopTypeDelConfirmDialog1(Context context, String id, String type, ShopTypeUpdateDialog1 dialog) {
        super(context);
        this.context = context;
        this.id = id;
        this.type = type;
        this.dialogs = dialog;
    }

    @Override
    public View onCreateView() {
        widthScale(0);
        //showAnim(new Swing());
        //dismissAnim(this, new ZoomOutExit());
        View inflate = View.inflate(mContext, R.layout.dialog_del_confirm, null);
        idTvRight = (TextView) inflate.findViewById(R.id.id_tv_del);
        idTvDelInfo = ((TextView) inflate.findViewById(R.id.id_edt_type_name));
        idTvClose = ((TextView) inflate.findViewById(R.id.id_tv_close));
        idImgClose = ((ImageView) inflate.findViewById(R.id.id_img_close));
        idTvDelInfo.setText("请确认删除，删除后数据不可恢复？");
        //设置背景颜色，和背景的透明圆弧角度
        inflate.setBackgroundDrawable(
                CornerUtils.cornerDrawable(Color.parseColor("#f8f9fa"), dp2px(10)));
        InputMethodUtils.closeSoftKeyboard((BaseActivity) context);
        return inflate;
    }

    @Override
    public void setUiBeforShow() {
        idTvRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (type.equals(ShopTypeActivity.PT_SHOP)) {
                    delType(Api.PT_SHOP_TYPE_DEL);
                } else if (type.equals(ShopTypeActivity.TIME_SHOP)) {
                    delType(Api.TIME_SHOP_TYPE_DEL);
                } else if (type.equals(IntegralTypeActivity.INTEGRAL)) {
                    delType(Api.APP_DEL_INTEGRAL_CATEGORY);
                }
                dismiss();
            }
        });
        idImgClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                dialogs.dismiss();
            }
        });
        idTvClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                dialogs.dismiss();
            }
        });
    }

    //删除分类
    private void delType(String url) {
        HttpRequest.postUrl(url)
                .addParams("id", id)
                .execute(new PostProcess.StringCallBack() {
                    @Override
                    public void onResponse(String json) {
                        boolean checkCode = Utils.checkCode(context, json);
                        if (checkCode) {
                            EventBus.getDefault().post(new Event.TypeEvent(CommonConstants.MAX_TYPE, true));
                            EventBus.getDefault().post(new Event.TypeEvent(CommonConstants.SHOP_LIST_REFRESH, true));
                        }
                        dialogs.dismiss();
                    }

                    @Override
                    public void onError(Exception e) {
                        ToastUtils.showToast(context, "删除分类失败");
                    }
                });

    }
}